package com.springboot.cloud.demos.ribbon.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.exception.SystemErrorType;
import com.springboot.cloud.demos.entity.form.*;
import com.springboot.cloud.demos.entity.param.MessageQueryParam;
import com.springboot.cloud.demos.entity.po.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.client.RestTemplate;

@Service
public class MessageService {

    @Autowired
    RestTemplate restTemplate;

    public Result getFallback(String levelId) {
        return Result.fail(SystemErrorType.SYSTEM_BUSY);
    }

    @SentinelResource(fallback = "getFallback")

    public Result get(String id) {
        return restTemplate.getForEntity("http://producer/message/{1}", Result.class, id).getBody();
    }

    @SentinelResource(fallback = "saveFallback")
    public Result<Message> save(@RequestBody MessageForm form) {
        return restTemplate.postForEntity("http://producer/message", form, Result.class).getBody();
    }


    @SentinelResource(fallback = "conditionsFallback")
    public Result<Page> list(@RequestBody MessageQueryForm param) {
        return restTemplate.postForEntity("http://producer/message/list", param, Result.class).getBody();
    }
    @SentinelResource(fallback = "conditionsFallback")
    public Result count(MessageQueryForm param) {
        return restTemplate.postForEntity("http://producer/message/count", param, Result.class).getBody();
    }

    @SentinelResource(fallback = "updateFallback")
    public Result update(@RequestBody Message form) {
        restTemplate.put("http://producer/message/{1}",form,form.getId());
        return Result.success();
    }
}
